package com.example.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.entity.AccDept;
import com.example.mapper.AccDeptMapper;
import com.example.service.AccDeptService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zk
 * @since 2019-07-01
 */
@Service
public class AccDeptServiceImpl extends ServiceImpl<AccDeptMapper, AccDept> implements AccDeptService {

    @Autowired
    private AccDeptMapper accDeptMapper;

    @Override
    public PageInfo<AccDept> listAccDept(Map<String, Object> params) {
        return new PageInfo<>(baseMapper.selectList(new EntityWrapper<AccDept>()
                .eq(!StringUtils.isEmpty(params.get("deptId")), "dept_id", params.get("deptId"))
                .like(!StringUtils.isEmpty(params.get("deptName")),
                        "dept_name", String.valueOf(params.get("deptName")))
                .orderBy("length(dept_name)")));
    }

    @Override
    public List<AccDept> selectList(Wrapper<AccDept> wrapper) {
        return super.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean UpdateAccDeptList(List<AccDept> accDepts) {
        return baseMapper.UpdateAccDeptList(accDepts) >= 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean FirstInsert(List<AccDept> accDeptList) {
        super.delete(null);
        return accDeptMapper.insertAccDeptList(accDeptList) > 0;
    }

    @Override
    public PageInfo<Map<String, Object>> getSelect(String deptName, Integer num) {
        List<Map<String, Object>> list = accDeptMapper.selectAccDept(deptName, num);
        return new PageInfo<>(list);
    }

    @Override
    public void deleteByDeptId(Integer deptId) {
        accDeptMapper.deleteByDeptId(deptId);
    }

    @Override
    public boolean saveBatch(List<AccDept> accDeptList) {
        return  this.saveBatch(accDeptList);
    }
    @Override
    public boolean saveOne(AccDept accDept) {
        return this.insert(accDept);
    }

    @Override
    public List<AccDept> getAll() {
        return accDeptMapper.getAll();
    }

    @Override
    public AccDept getDeptWithNoLogic(String deptName) {
        List<AccDept> deptWithNoLogic = accDeptMapper.getDeptWithNoLogic(deptName);
        if (CollectionUtils.isEmpty(deptWithNoLogic)) {
            return null;
        }
        Optional<AccDept> first = deptWithNoLogic.stream().filter(x -> x.getIsDel() != null && x.getIsDel().equals(0)).findFirst();
        return first.orElseGet(() -> deptWithNoLogic.get(0));
    }
}
